<?php

namespace App\Http\Controllers\User;

use App\Http\Controllers\BaseController;
use App\Models\Article;
use App\Models\User;
use App\Models\UserFollow;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class FollowController extends BaseController
{
    public function article(Request $request)
    {

        $params = $this->validate($request,[
            'keyword' => 'string|max:100'
        ]);

        $user_id = auth('api')->id();

        $friends = DB::table('user_follows')->select(['friend_id'])->where('user_id',$user_id)->get();


        if ($friends->isEmpty()){
            return  $this->success();
        }

        $friend_ids = array_column($friends->toArray(),'friend_id');




       $articles =  Article::query()
              ->with(['user:id,avatar,nickname,user_title_id,auth' => ['title:id,title']])
              ->selectRaw('articles.*,(select count(id) from user_like_articles where article_id=articles.id and status=1) as like_count,
                    (select count(friend_id) from user_follows where status=1 and friend_id=articles.user_id and user_id='.$user_id.') as is_follow,
                    (select count(id) from user_collect_articles where article_id=articles.id and status=1 and user_id='.$user_id.') as is_collect,
                    (select count(id) from user_like_articles where article_id=articles.id and status=1 and user_id='.$user_id.') as is_like,
                    (select count(id) from user_like_articles where user_like_articles.status=1 and article_id=articles.id) as like_count,
                    (select count(id) from user_collect_articles where status=1 and article_id=articles.id) as collect_count,
                    (select count(id) from article_comments where article_id=articles.id and audit_status=2) as comment_count,
                    (select sum(share_count) from article_shares where article_id=articles.id) as share_count')
            ->orderByDesc('id')
            ->whereIn('user_id',$friend_ids)
           ->orderByDesc('id')
            ->paginate(10);


       return $this->success($articles);



        $query  =   UserFollow::query()
            ->with(['friend' =>function($q){
                $q->selectRaw('id,avatar,nickname,user_title_id,auth,
                (select count(id) from user_follows where status=1 and user_id=users.id) as fan_count,
                (select profession_id from firms where user_id=users.id limit 1) as profession_id,
                (select count(id) from user_like_articles where status=1 and article_id in (select id from articles where user_id=users.id)) as like_count')
                    ->with('title:id,title');
            }])->where('user_id',$user_id)
            ->where('status',1);
//            ->with(['article' => function($q) use ($user_id){
//                $q->selectRaw('*,(select count(id) from user_like_articles where article_id=articles.id and status=1) as like_count,
//                (select count(friend_id) from user_follows where status=1 and friend_id=articles.user_id and user_id='.$user_id.') as is_follow,
//                    (select count(id) from user_collect_articles where article_id=articles.id and status=1 and user_id='.$user_id.') as is_collect,
//                    (select count(id) from user_like_articles where article_id=articles.id and status=1 and user_id='.$user_id.') as is_like,
//                    (select count(id) from user_like_articles where user_like_articles.status=1 and article_id=articles.id) as like_count,
//                    (select count(id) from user_collect_articles where status=1 and article_id=articles.id) as collect_count,
//                    (select count(id) from article_comments where article_id=articles.id and audit_status=2) as comment_count,
//                    (select sum(share_count) from article_shares where article_id=articles.id) as share_count');
//            }]);

        if (isset($params['keyword'])){
            $keyword = $params['keyword'];
            $query->whereHas('friend.article',function ($q) use ($keyword){
                $q->where('title','like','%'.$keyword.'%')->orderByDesc('id');
            });
        }else{
            $query->whereHas('friend.article',function ($q){
                $q->orderByDesc('id');
            });
        }

        $data = $query
            //->orderByDesc('id')
            ->simplePaginate(10);

        foreach ($data->items() as $item){

            if ($item->friend){
                $item->article = $this->getArticles($user_id,$item->friend->id);
                if ($item->article){
                    $item->article->media_url = json_decode($item->article->media_url,true);
                }

            }

        }


        return $this->success($data);
    }


    public function getArticles($user_id,$auser_id)
    {
       $articles =  DB::table('articles')->selectRaw('*,(select count(id) from user_like_articles where article_id=articles.id and status=1) as like_count,
                (select count(friend_id) from user_follows where status=1 and friend_id=articles.user_id and user_id='.$user_id.') as is_follow,
                    (select count(id) from user_collect_articles where article_id=articles.id and status=1 and user_id='.$user_id.') as is_collect,
                    (select count(id) from user_like_articles where article_id=articles.id and status=1 and user_id='.$user_id.') as is_like,
                    (select count(id) from user_like_articles where user_like_articles.status=1 and article_id=articles.id) as like_count,
                    (select count(id) from user_collect_articles where status=1 and article_id=articles.id) as collect_count,
                    (select count(id) from article_comments where article_id=articles.id and audit_status=2) as comment_count,
                    (select sum(share_count) from article_shares where article_id=articles.id) as share_count')
            ->orderByDesc('id')
           ->where('user_id',$auser_id)
            ->first();

       return $articles;
    }


    public function index(Request $request)
    {
        $params = $this->validate($request,[
            'keyword' => 'string|max:100'
        ]);


        $user_id = auth('api')->id();

        $query = UserFollow::query();

        if (isset($params['keyword'])){
            $keyword = $params['keyword'];
            $query->whereHas('friend',function ($q) use ($keyword){
                $q->where('nickname','like','%'.$keyword.'%');
            });
        }


        $data = $query->with(['friend' => function($q){
                                    $q->with(['title:id,title'])
                                        ->selectRaw('id,avatar,nickname,user_title_id,auth,
                                        (select count(id) from user_follows where status=1 and friend_id=users.id) as fan_count,
                                        (select profession_id from firms where user_id=users.id limit 1) as profession_id,
                                         (select count(id) from user_like_articles where status=1 and article_id in (select id from articles where user_id=users.id)) as like_count
                                        ');
                             }])->where('user_id',$user_id)
                            ->where('status',1)
                            ->orderByDesc('id')
                            ->simplePaginate(10);


        return $this->success($data);
    }

    public function not(Request $request)
    {

        $params = $this->validate($request,[
            'keyword' => 'string|max:100'
        ]);

        $user_id = auth('api')->id();
        $friends = UserFollow::query()->select(['friend_id'])
                ->where('status',1)
                ->where('user_id',$user_id)->get();
        $ids = array_column($friends->toArray(),'friend_id');
        array_push($ids,$user_id);
        $query = User::query()
            ->selectRaw('users.*,
            (select count(id) from user_follows where status=1 and friend_id=users.id) as fan_count,
              (select profession_id from firms where user_id=users.id limit 1) as profession_id,
            (select count(id) from user_like_articles where status=1 and article_id in (select id from articles where user_id=users.id)) as like_count'
            )->whereNotIn('id',$ids);


        if (isset($params['keyword'])){
            $keyword = $params['keyword'];
            $query->where('nickname','like','%'.$keyword.'%');
        }


        $data =  $query->orderByDesc('id')
                    ->simplePaginate(10);

       return $this->success($data);
    }


    public function store(Request $request)
    {
        $param = $this->validate($request,[
            'friend_id' => 'required'
        ]);

        $user_id = auth('api')->id();


        $status = DB::table('user_follows')
            ->where([
                'user_id' => $user_id,
                'friend_id' => $param['friend_id']
            ])->value('status');

        if (empty($status)){
            $result =  DB::table('user_follows')->insert([
                'user_id' => $user_id,
                'friend_id' => $param['friend_id'],
                'status' => 1
            ]);
            return $result ? $this->success() :$this->error();
        }

        $status = $status==1 ? 2 : 1;
        $result = DB::table('user_follows')->where([
            'user_id' => $user_id,
            'friend_id' => $param['friend_id']
        ])->update(['status' => $status]);

        return $result ? $this->success() : $this->error();

    }
}
